#!/bin/bash

# poiformat
# File ID: bcf111dc-7683-11e0-9799-01b466beaad8

progname=poiformat

if [ "$1" = "-g" ]; then
    shift
else
    cat "$@" | xf | roundgpx | latlon | wpt-dupdesc | wpt-rmsym | xf | sortxml -r -s wpt -u
    exit
fi

files="$@"
branch=$progname.$(date -u +"%Y%m%dT%H%M%SZ")

commit() {
    msg="$*"
    stat1=$(git status --short $files)
    if [ -z "$stat1" ]; then
        echo $progname: No changes, skipping commit \"$msg\"
        return;
    fi
    git add $files
    git commit -m "$msg$(echo; echo; suuid -t c_poiformat -c "$msg")"
}

git checkout -b $branch
filt xf $files || {
    echo $progname: XML error >&2;
    git checkout -;
    git branch -d $branch;
    exit 1;
}
commit "$progname filtered files through xf"
filt roundgpx $files
commit "$progname filtered files through roundgpx"
filt latlon $files
commit "$progname filtered files through latlon"
filt wpt-dupdesc $files
commit "$progname filtered files through wpt-dupdesc"
filt wpt-rmsym $files
commit "$progname filtered files through wpt-rmsym"
filt xf $files
commit "$progname filtered files through xf"
filt "sortxml -r -s wpt" $files
commit "$progname filtered files through sortxml -r -s wpt"
filt "sortxml -r -s wpt -u" $files
commit "$progname removed duplicate waypoints"
git checkout -
git merge --no-ff --edit -m "Merge branch '$branch' created by $progname" $branch
git branch -d $branch
